package blog.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import blog.dao.ArticletypeRespository;
import blog.dao.UserRepository;
import blog.entity.Articletype;
import blog.entity.User;
import jakarta.transaction.Transactional;

@Service
@Transactional
public class UserService {
    @Autowired
    UserRepository userRespository;
    
    /**
     * 查找所有
     * @return
     */
    public List<User> findAll() {
	return userRespository.findAll();
    }
    
    /**
     * 分页查找
     * @return
     */
    public Page<User> findAllByPage(int pageIndex, int pageSize) {
        //排序
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        //分页
        Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
        //分页查询
        Page<User> userPage = userRespository.findAll(pageable);
        return userPage;
    }
    
    /**
     * 查找某条记录
     * 
     * @param username, pwd
     * @return
     */
    public User login(String username, String pwd) {
	    return userRespository.findByUsernameAndPwd(username, pwd);
    }
}
